package com.nisco.dms.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.nisco.dms.entity.Company;
import com.nisco.dms.repository.CompanyDao;
import com.nisco.dms.repository.mybatis.CompanyMyBatisDao;
import com.nisco.dms.util.dwz.Page;
import com.nisco.dms.util.dwz.PageUtils;

/**
 * 
 * @author Robin
 * 
 */
@Component
@Transactional(readOnly = true)
public class CompanyService extends BaseService<Company, String> {

	private CompanyDao companyDao;
	
	private CompanyMyBatisDao companyMyBatisDao;

	public CompanyService() {

	}

	//自动对方法的入参进行标注
	@Autowired
	public CompanyService(CompanyDao companyDao) {
		super((JpaRepository<Company, String>)companyDao);
		this.companyDao = companyDao;
	}

	@Autowired
	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	
	@Autowired
	public void setCompanyMyBatisDao(CompanyMyBatisDao companyMyBatisDao) {
		this.companyMyBatisDao = companyMyBatisDao;
	}

	/**
	 * 查询分页
	 * 
	 * @param compId
	 * @param page
	 * @return
	 */
	public List<Company> find(String compNo, Page page) {
		org.springframework.data.domain.Page<Company> p = companyDao
				.findByStrSnContaining(compNo, PageUtils.createPageable(page));
		PageUtils.injectPageProperties(page, p);
		return p.getContent();
	}

	/**
	 * 进阶查询
	 * 
	 * @param compId
	 * @param compName
	 * @param memo
	 * @param page
	 * @return
	 */
	public List<Company> find(String compNo, String compName, String memo,
			Page page) {
		org.springframework.data.domain.Page<Company> p = null;
		if (StringUtils.isBlank(memo)) {
			p = companyDao.findByStrSnContainingAndStrNameContaining(compNo, compName,
					PageUtils.createPageable(page));
		} else {
			p = companyDao.findByStrSnContainingAndStrNameContainingAndStrDescContaining(compNo,
					compName, memo, PageUtils.createPageable(page));
		}
		PageUtils.injectPageProperties(page, p);
		return p.getContent();
	}
	
	//mybatis
	/**
	 * 返回总条数
	 * @param param
	 * @return
	 */
	public int searchTotalMybatis(String param) {
		Map<String, Object> parameters = Maps.newHashMap();
		parameters.put("strSn", param);
		parameters.put("strName", param);
		parameters.put("memo", param);
		return companyMyBatisDao.search(parameters).size();
	}
	
	/**
	 * 返回总条数
	 * @param company
	 * @return
	 */
	public int searchTotalMybatis(Company company) {
		Map<String, Object> parameters = Maps.newHashMap();
		parameters.put("strSn", company.getStrSn());
		parameters.put("strName", company.getStrName());
		return companyMyBatisDao.search(parameters).size();
	}
	
	public List<Company> searchMybatis(Company company,String keywords,Page page) {
		Map<String, Object> parameters = Maps.newHashMap();
		parameters.put("strSn", company.getStrSn());
		parameters.put("strName", company.getStrName());
		if( keywords != null){
		parameters.put("keywords", keywords);
		}else{
			parameters.put("keywords", null);
		}
		parameters.put("start", (page.getPageNum()!=1?page.getPageNum()-1:0)*page.getNumPerPage());
		parameters.put("end", page.getNumPerPage());
		return companyMyBatisDao.searchByPage(parameters);
	}
	/**
	 * 查询分页
	 * 
	 * @param compId
	 * @param compName
	 * @param page
	 * @return
	 */
	// public List<Company> find(Long compId, String compName, Page page) {
	// org.springframework.data.domain.Page<Company> p =
	// companyDao.findByCompIdAndCompName(compId, compName,
	// PageUtils.createPageable(page));
	// PageUtils.injectPageProperties(page, p);
	// return p.getContent();
	// }
	
}
